package net.googlecode.garzha.controllers;

import net.googlecode.garzha.domains.Comment;
import net.googlecode.garzha.domains.Group;
import net.googlecode.garzha.domains.Person;
import net.googlecode.garzha.services.PersonService;
import net.googlecode.garzha.services.SecurityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
/**
 * Controller user page.
 */
public class UserController {

    @Autowired
    private PersonService personService;
    @Autowired
    private SecurityService securityService;

    protected static final Logger logger = Logger.getLogger(IndexController.class);
    /**
     * Save current id.
     */
    String idcurrent = "";


    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ModelAndView handleRequest(String id, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.info(id + " USER Get current person");

        idcurrent = id;
        logger.info("UUSERR Get current person");
        Person prs = personService.getPersonID(id);
        Person my = personService.getPerson(securityService.getCurrentUser());
        Group gr = personService.getGroup(prs.getGrpid());
        logger.info(my.getId());
        logger.info(prs.getId());
        /**
         * If a person chooses his id, then he go to the home page.
         */
        if (my.getId().equals(prs.getId())) {
            logger.info("USER ==");
            response.sendRedirect("home");
        }
        ModelAndView mv = new ModelAndView("user");
        logger.info("Set parametres that send to view");
        mv.addObject("groupmates", personService.getGroupMates(prs));
        mv.addObject("info", prs);

        logger.info("enrate " + my.getEnrate());
         mv.addObject("com",personService.getListComment(prs.getId().toString()));
        mv.addObject("rate", my.getEnrate());
        mv.addObject("infogr", gr.getName());
        mv.addObject("root", request.getContextPath());
        mv.addObject("top5", personService.getTop5());
        mv.addObject("my", my.getId());
        mv.addObject("nomy", prs.getId());
        logger.info("Returning user page view");
        return mv;


    }

    /**
     * Controller to increase the rating.
     *
     * @param person
     * @param result
     * @return
     */
    @RequestMapping(value = "/uprate", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("person") Person person,
                             BindingResult result) {
        logger.info("Uprate ");


        Person prs1 = personService.getPerson(securityService.getCurrentUser());
/**
 * Check the remaining number of votes in person.
 */
        if (prs1.getEnrate() != 0) {


            prs1.setEnrate(prs1.getEnrate() - 1);
            personService.updatePerson(prs1);
            Person prs = personService.getPersonID(idcurrent);
            prs.setRate(1 + prs.getRate());

            personService.updatePerson(prs);
            logger.info("REGISTER return function ");
        }

        return "redirect:/home";
    }
   @RequestMapping(value = "/addcommentuser", method = RequestMethod.POST)
    public String setPas(@RequestParam(value = "comment", required = true) String comment

                       ) {
        logger.info("pas add function ");
        Person prs = personService.getPerson(securityService.getCurrentUser());
        Comment com = new Comment();
       com.setComment(prs.getFname()+prs.getSname()+": "+comment );
       com.setPersonid(idcurrent);
       com.setGuestid(prs.getId().toString());

        personService.addComment(com);
        logger.info("set return function ");
        return "redirect:/user?id="+idcurrent;
    }

}